﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HR60_PanelSoft.View.Base;
using HR60_PanelSoft.View.TabForms.ListItems;
using HR60_PanelSoft.Model;
using HR60_PanelSoft.Model.BaseModel;

namespace HR60_PanelSoft.View.TabForms
{
    //GC:[g1]修改外部信号面板bug [PanelForm_Signal] @s3
    //GC:[g2]保存到内存对象[PanelForm_Signal] @s3
    //GC:[g3]优化UI[PanelForm_Signal][s]
    //GC:[g4]bug2192/2203/2204/2205[PanelForm_Signal]
    //GC:[g5]bug2613[PanelForm_Signal]
    //GC:[g6]bug2759[PanelForm_Signal]
	public partial class PanelForm_Signal : BaseForm
	{
        /* s1 将panel变量提升为字段 */
        Model.Panel _cur_panel = new Model.Panel();
        /* end of s1 */
		public PanelForm_Signal()
		{
			InitializeComponent();

            /* [g3.s1] 设备列表显示不全  */
            this.divDevice.AutoScroll = true;
            /* end of [g3.s1] */

			// 外部信号定义列表
			IDictionary  sconstMap = new Dictionary<string, SignalConst>();
			sconstMap.Add("未定义", SignalConst.NULL);
			sconstMap.Add("常开", SignalConst.OPEN);
			sconstMap.Add("常闭", SignalConst.CLOSE);
			selSconst.bindData(sconstMap);

			// 关联亮度列表
			IDictionary brightnessMap = new Dictionary<string, int>();
			for (int i = 1; i <= 7; i++)
			{
				brightnessMap.Add("亮度" + i, i);
			}
			selBrightness.bindData(brightnessMap);

            /* [g4.s1] */
            selBrightness.Value = 3;

            txtDelay.Value = "5";
            /* end of [g4.s1] */

		}

		// 绑定数据
		public override void refreshFormData(System.Collections.Generic.IDictionary<string, object> dataMap)
		{
            _cur_panel = (HR60_PanelSoft.Model.Panel)dataMap["panel"];

            /* [g5.s1]bug2613,此处关联有误 */
            //selScene.Value = _cur_panel.signal.sconst;
            selSconst.Value = _cur_panel.signal.sconst;
            /* end of [g5.s1] */
            selBrightness.Value = _cur_panel.signal.brightness;
            txtDelay.Value = _cur_panel.signal.delay;

			// 场景列表
			selScene.cleanItems();
            /* [g5.s2]bug2613 */
            selScene.setStaticFirstItem("选择关联场景");

            /* end of [g5.s2] */

			selScene.bindData((IList)scenes, "name", "uuid");

            if (_cur_panel.signal.scene != null)
			{
				//radioScene.Checked = true;
                selScene.Value = _cur_panel.signal.scene;
			}
            /* [g6.s3]刷新关联设备 */
            else if (_cur_panel.signal.localScene != null)
            {
                sceneDeviceList.init(_cur_panel);
                sceneDeviceList.DeviceEventChange += new HR60_PanelSoft.View.TabForms.Handles.DeviceEventChangeHandle(sceneDeviceList_DeviceEventChange);
            }


			// 负载设备列表
            //sceneDeviceList.init(_cur_panel);
            //sceneDeviceList.DeviceEventChange += new HR60_PanelSoft.View.TabForms.Handles.DeviceEventChangeHandle(sceneDeviceList_DeviceEventChange);
            /* end of [g6.s3] */

            /* [g6.s1] */
            //if (_cur_panel.signal.scene == null)
            //{
            //    radioScene.Checked = true;
            //}
            //else
            //{
            //    radioDevice.Checked = true;
            //}
            if(_cur_panel.signal.scene != null)
			{
				radioScene.Checked = true;
			}
            else if (_cur_panel.signal.localScene != null)
            {
                radioDevice.Checked = true;
            }
            else
            {
                radioScene.Checked = true;
            }
            /* end of [g6.s1] */

		}

		// 场景事件更改
		void sceneDeviceList_DeviceEventChange(Device tagDevice, SceneEvent sceneEvent)
		{

		}

		// 数据修改事件，基础信息
		public override void onFormChanged(BaseFormControl sender, System.EventArgs e)
		{
            /* s3 保存到内存 */
            int delay_temp = -1;
            if (!int.TryParse((string)txtDelay.Value, out delay_temp))
            {
                /* [g4.s2] */
                delay_temp = 5; /*默认100--> 改为5*/
                txtDelay.Value = "5";

                /* end of [g4.s2] */
            }
            /* [g4.s3] */
            if (delay_temp < 5 || delay_temp > 250)
            {
                //delay_temp = 5;
                //txtDelay.Value = "5";
                View.ViewUtils.showAlert("延迟时间设置区间为5秒至250秒！");
            }

            /* end of [g4.s3] */

            _cur_panel.signal.sconst = (SignalConst)selSconst.Value;
            _cur_panel.signal.brightness = (int)selBrightness.Value;
            _cur_panel.signal.delay = delay_temp;
            /* [g6.s2] */
            //if(radioScene.Checked)
            //    _cur_panel.signal.scene = (string)selScene.Value;
            //else if (radioDevice.Checked) /* 保存关联设备，以场景形式保存 */
            //{
            //    /* 
            //     * 此处关联的设备，是当前面板所关联的设备，
            //     * 将这些设备的编辑，封装成Scene，即所谓的
            //     * localScene。
            //     */
            //    _cur_panel.signal.localScene = (Scene)this.sceneDeviceList.Value;
            //}
            if (radioScene.Checked)
            {
                _cur_panel.signal.scene = (string)selScene.Value;
                _cur_panel.signal.localScene = null;
            }
            else if (radioDevice.Checked)
            {
                _cur_panel.signal.localScene = (Scene)this.sceneDeviceList.Value;
                _cur_panel.signal.scene = null;
            }

            /* end of [g6.s2] */
            /* end of s3 */
		}

		private void checkedChanged(object sender, EventArgs e)
		{
			divScene.Visible = radioScene.Checked;
			divDevice.Visible = radioDevice.Checked;

            /* s2 使能动态刷新 */
            if (divDevice.Visible && null != _cur_panel)
                sceneDeviceList.refreshPinDevList(_cur_panel);
            /* end of s2 */
                
		}
	}
}
